{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python继承组合\n",
    "\n",
    "### 什么是继承？\n",
    "你父亲有的你也有，但你有的特有属性和方法你父亲就没有。\n",
    "\n",
    "### 1、 继承父类的属性与方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Stock:\n",
    "    def __init__(self,stockName, price, volume):\n",
    "        self.stockName=stockName\n",
    "        self.price = price\n",
    "        self.volume = volume\n",
    "    def calAmount(self):\n",
    "        return {self.stockName+'_Amount': self.price*self.volume}\n",
    "    \n",
    "class BABA(Stock):\n",
    "    pass\n",
    "\n",
    "b = BABA('BABA', 180, 30000).calAmount()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'BABA_Amount': 5400000}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2、 属性覆盖\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Stock:\n",
    "    def __init__(self,stockName, price, volume):\n",
    "        self.stockName=stockName\n",
    "        self.price = price\n",
    "        self.volume = volume\n",
    "    def calAmount(self):\n",
    "        return {self.stockName+'_Amount': self.price*self.volume}\n",
    "\n",
    "class BABA(Stock):\n",
    "    pass\n",
    "\n",
    "class TENCETN(Stock):\n",
    "    pass\n",
    "\n",
    "class BIDU(Stock):\n",
    "    pass\n",
    "\n",
    "class BuyMaxAmount(Stock):\n",
    "    def __init__(self):\n",
    "        self.haveCapital = True\n",
    "    def buy(self):\n",
    "        if self.haveCapital:\n",
    "            print('I have cash to buy stock')\n",
    "            self.haveCapital = False\n",
    "        else:\n",
    "            print(\"I don't have any cash now\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "__init__() takes 1 positional argument but 4 were given",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-64ba8ab1d9a5>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mBuyMaxAmount\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'TENCETN'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m350\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m20000\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcalAmount\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: __init__() takes 1 positional argument but 4 were given"
     ]
    }
   ],
   "source": [
    "BuyMaxAmount('TENCETN', 350, 20000).calAmount()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3、 Super函数\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Stock:\n",
    "    def __init__(self,stockName, price, volume):\n",
    "        self.stockName=stockName\n",
    "        self.price = price\n",
    "        self.volume = volume\n",
    "    def calAmount(self):\n",
    "        return {self.stockName+'_Amount': self.price*self.volume}\n",
    "\n",
    "class BABA(Stock):\n",
    "    pass\n",
    "\n",
    "class BuyAmount(Stock):\n",
    "    def __init__(self, stockName, price, volume):\n",
    "        self.haveCapital = True\n",
    "        super().__init__(stockName, price, volume)\n",
    "    def buy(self):\n",
    "        if self.haveCapital:\n",
    "            print('I have cash to buy stock')\n",
    "            self.haveCapital = False\n",
    "        else:\n",
    "            print(\"I don't have any cash now\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "buyBABA = BuyAmount('BABA', 2500, 50000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I have cash to buy stock\n"
     ]
    }
   ],
   "source": [
    "buyBABA.buy()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4、 类组合应用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class StockA:\n",
    "    def __init__(self,price):\n",
    "        self.price = price\n",
    "\n",
    "class StockB:\n",
    "    def __init__(self, price):\n",
    "        self.price = price\n",
    "\n",
    "class TwoStock:\n",
    "    def __init__(self, priceA, priceB):\n",
    "        self.stockA = StockA(priceA)\n",
    "        self.stockB = StockB(priceB)\n",
    "    def printStocks(self):\n",
    "        print('StockA $ %d , StockB $ %d '% (self.stockA.price, self.stockB.price))\n",
    "\n",
    "twoStock = TwoStock(220, 350)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "StockA $ 220 , StockB $ 350 \n"
     ]
    }
   ],
   "source": [
    "twoStock.printStocks()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
